package com.nuoniu.sibanyun.controller.system;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.SysEmployee;
import com.nuoniu.sibanyun.entity.vo.SysEmployeeSimpleVo;
import com.nuoniu.sibanyun.service.ISysEmployeeService;
import com.nuoniu.sibanyun.service.impl.SysUserServiceImpl;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.nuoniu.sibanyun.common.BaseController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//*
// * <p>
// * 员工表 前端控制器
// * </p>
// *
// * @author baiqiang
// * @since 2020-05-29


@RestController
@RequestMapping("/sys/employee")
@Api(tags = "员工管理", value = "员工管理")
public class SysEmployeeController extends BaseController {

    @Autowired
    private ISysEmployeeService employeeService;
    @Autowired
    private SysUserServiceImpl sysUserService;

    @ApiOperation("获取员工列表")
    @GetMapping("pageList")
    public HttpResult pageList(Page page) {
        return HttpResult.ok(employeeService.pageList(page));
    }
//    @ApiOperation("获取员工列表")
//    @GetMapping("pageList")
//    public HttpResult pageList(Page page) {
//        return HttpResult.ok(sysUserService.pageList(page));
//    }

//*
//     * 获取员工信息
//     *
//     * @author 小啊刚
//     * @date 2020-09-05


    @ApiOperation("获取员工信息")
    @GetMapping("info")
    public HttpResult getEmployee() {
        UserInfo userInfo = UserThreadLocal.get();
        SysEmployee employee = employeeService.getByUserId(userInfo.getUserId().longValue());
        return HttpResult.ok(employee);
    }


//*
//     * 保存员工
//     *
//     * @author 小啊刚
//     * @date 2020-09-05


    @ApiOperation("保存员工")
    @PostMapping("save")
    public HttpResult saveEmployee(@RequestBody SysEmployee employee) {
        UserInfo userInfo = UserThreadLocal.get();
        if (employee.getId() != null) {
            employeeService.updateById(employee);
        } else {
            employee.setTenantId(userInfo.getCompanyId().longValue());
            employee.setTenantName(userInfo.getCompanyName());
            employeeService.save(employee);
        }
        return HttpResult.ok();
    }


    @ApiOperation("下载导入数据模板")
    @GetMapping(value = "importTemplate")
    public void importTemplate(HttpServletResponse response) {
        employeeService.importTemplate(response);
    }

    @ApiOperation("导入员工")
    @PostMapping("importEmployee")
    public HttpResult importEmployee(MultipartFile file) throws IOException, NoSuchFieldException {
        return HttpResult.ok(employeeService.importEmployee(file));
    }

    @ApiOperation("通过 机构ID(部门ID) 分页查询员工信息")
    @GetMapping("pageSysEmployeeSimpleVo")
    public HttpResult<List<SysEmployeeSimpleVo>> pageSysEmployeeSimpleVo(Page page, Integer officeId, String searchKey) {
        return HttpResult.ok("", employeeService.pageSysEmployeeSimpleVo(page, officeId, searchKey));
    }


    @ApiOperation("删除未注册员工")
    @DeleteMapping("delete")
    public HttpResult delete(Long id) {
        employeeService.delete(id);
        return HttpResult.ok();
    }

    @ApiOperation("一键邀请注册")
    @PostMapping("inviteToRegister")
    public HttpResult inviteToRegister(@RequestBody String[] userPhones) {
        if (userPhones.length < 1 || userPhones.length > 100) {
            throw new NuoNiuException("一次只能邀请1-100位员工注册");
        }
        employeeService.inviteToRegister(userPhones);
        return HttpResult.ok();
    }
}
